package com.example.demo.user.web;

import com.example.demo.user.entity.User;
import com.example.demo.user.service.UserService;
import com.example.demo.utils.RespMessage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService service;

    /**
     * 查询所有用户
     * @return
     */
    @RequestMapping("/list")
    public RespMessage findList(User user){
        long start = System.currentTimeMillis();
        PageHelper.startPage(1, 2);
        List<User> list = service.findList(null);
        PageInfo pageInfo = new PageInfo(list);
        long end = System.currentTimeMillis();
        System.out.println(end - start + "ms");
        return RespMessage.success("成功", list);
    }


    /**
     * 查询所有单个用户
     * @return
     */
    @RequestMapping("/get")
    public RespMessage findOne(String id){

        service.findOne(id);


        return RespMessage.success("成功");
    }

    /**
     * 删除单个用户
     * @return
     */
    @RequestMapping("/remove")
    public RespMessage remove(String id){

        service.remove(id);

        return RespMessage.success("成功");
    }

    /**
     * 添加用户
     * @return
     */
    @RequestMapping("/save")
    public RespMessage save(User user){

        service.save(user);

        return RespMessage.success("成功");
    }

    /**
     * 修改用户
     * @return
     */
    @RequestMapping("/update")
    public RespMessage update(User user){

        service.update(user);

        return RespMessage.success("成功");
    }

}
